package mya.mes.mdc.model.tag.values;


import com.alibaba.fastjson.JSONObject;
import mya.mes.mdc.model.plc.TagPrefix;
import mya.mes.mdc.model.tag.MdcTagValue;
import mya.mes.mdc.model.tag.util.TagHolder;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MdcValuesConverter {

    public static MdcTagValues convert(String tagPrefix, Long mqttMsgId, JSONObject json) {
        final boolean isValueStr = TagPrefix.isString(tagPrefix);
        final boolean isValueInt = TagPrefix.isNumber(tagPrefix);
        final boolean isValueBool = TagPrefix.isBoolean(tagPrefix);

        if (!(isValueStr || isValueInt || isValueBool)) {
            throw new RuntimeException("不能识别的topic:" + tagPrefix);
        }

        MdcTagValues tagValues = new MdcTagValues();
        tagValues.dateTime = json.getString("DT");
        tagValues.mqttMsgId = mqttMsgId;

        json.remove("_id");
        json.remove("DT");
        json.remove("topicName");
        json.remove("SAVED_MQTT_COLL_NAME");

        TagHolder tagHolder = TagHolder.getInstance();

        List<MdcTagValue> valueListTmp = new ArrayList<>(json.size());

        json.keySet().stream().forEach(key -> {
            MdcTagValue tagValue = new MdcTagValue();
            tagValue.isValueStr = isValueStr;
            tagValue.isValueBool = isValueBool;
            tagValue.isValueInt = isValueInt;
            try {
                setTagValueData(tagValue, tagPrefix, key, tagHolder);
            } catch (Throwable t) {
                return;
            }

            tagValue.mqttMsgId = tagValues.mqttMsgId;
            tagValue.dateTime = tagValues.dateTime;

            if (isValueStr) {
                tagValue.strValue = json.getString(key);
            } else if (isValueInt) {
                String tmp = json.getString(key);
                if (tmp != null) {
                    tagValue.intValue = Integer.parseInt(tmp);
                }

            } else if (isValueBool) {
                String tmp = json.getString(key);
                if ("0".equals(tmp)) {
                    tagValue.boolValue = false;
                } else if ("1".equals(tmp)) {
                    tagValue.boolValue = true;
                } else {
                    tagValue.boolValue = Boolean.parseBoolean(tmp);
                }
            }
            valueListTmp.add(tagValue);
        });

        tagValues.valueList = Collections.unmodifiableList(valueListTmp);
        return tagValues;
    }

    private static void setTagValueData(MdcTagValue tagValue,
                                        String tagPrefix, String key,
                                        TagHolder tagHolder) {
        tagValue.tagPrefix = tagPrefix;
        try {
            tagValue.tagIndex = Integer.parseInt(key);
        } catch (NumberFormatException nfe) {
            throw new RuntimeException("tagIndex不是数字：" + key);
        }

        tagValue.tagName = tagPrefix + paddingKey(key);
        tagValue.mdcTag = tagHolder.getMdcTag(tagValue);
        if (tagValue.mdcTag == null) {
            throw new RuntimeException("[tagPrefix,tagIndex] =[ "
                    + tagPrefix + "," + tagValue.tagIndex
                    + "]无对应的MdcTag");
        }
    }

    private static String paddingKey(String key) {
        String key1;

        int keyLen = key.length();


        switch (keyLen) {

            case 1: {
                key1 = "000" + key;
                break;
            }
            case 2: {
                key1 = "00" + key;
                break;
            }
            case 3: {
                key1 = "0" + key;
                break;
            }
            case 4: {
                key1 = key;
                break;
            }
            default: {

                throw new RuntimeException("Tag长度不正确（ == 0 或者 > 4): " + keyLen);
            }
        }

        return key1;
    }


}
